﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Survey.Models.Where
{
    public class TemplateWhere:IEFWhere<Template>
    {
        public long? Id { get; set; }      
        /// <summary>
        /// 模块名称，用以区分模版归属哪个模块
        /// </summary>
        public string? Module { get; set; }
        /// <summary>
        /// 路由匹配时用以响应模版的条件
        /// </summary>
        public string? Url { get; set; }
        public int? IsUsing { get; set; }
        public int? Page { get; set; }
        public int? Limit { get; set; }

        public IQueryable<Template> Process(IQueryable<Template> query, out int count)
        {
            if (Id.HasValue)
            {
                query=query.Where(x=>x.Id == Id);
            }
            if(!string.IsNullOrEmpty(Module))
            {
                query = query.Where(x => x.Module == Module);
            }
            if (!string.IsNullOrEmpty(Url))
            {
                query=query.Where(x=>x.Url == Url);
            }
            if(IsUsing.HasValue)
            {
                query=query.Where(x=>x.IsUsing == IsUsing); 
            }
            count = query.Count();
            if (Limit.HasValue && Page.HasValue && Limit.Value > 0 && Page.Value > 0)
            {
                query = query.Skip((Page.Value - 1) * Limit.Value).Take(Limit.Value);
            }
            return query;
        }
    }
}
